// pages/list/list.js
const {shebeilist, scanCodeBangding,quxiao,shanchu,payment,lookwuliu} =require('../../util/api')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    list:[],
    zhuangtai:['全部','待付款','待发货','待安装','已安装','已取消'],
    etId:"",
   bianhao:"",
   memberId:"",
   recordId:"",
   pageNum:"",
   show:false,
   currentTab:"0",
   wuliulist:{},
   kuaidigongsi:"",
   kuaididanhao:"",

    
  },

  
  cp(e){
    wx.setClipboardData({
      data: this.data.kuaididanhao,
      success (res) {
        wx.getClipboardData({
          success (res) {
            wx.showToast({
              title: '复制成功',
              icon:'success'
            })
          }
        })
      }
    })
    console.log(e)

  },


  showModal: function () {
    // 显示遮罩层
     var animation= wx.createAnimation({
       duration: 200,
       timingFunction: "linear",
       delay: 0
     })
     this.animation = animation
     animation.translateY(300).step()
     this.setData({
       animationData: animation.export(),
       showModalStatus: true
     })
     setTimeout(function () {
       animation.translateY(0).step()
       this.setData({
         animationData: animation.export(),
         
       })
     }.bind(this), 200)
     this.setData({
      show:true
     })
    
   },
   hideModal: function () {
   
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
    this.setData({
      show:false
     })
    

  },
  chakanwuliu(e){

    this.showModal();
    
    console.log(e)
    
    this.setData({
      recordId:e.currentTarget.dataset.orderno.id,
     
    })
    let p={
      memberId:wx.getStorageSync('memberId'),
      recordId:this.data.recordId
    }
    console.log(p)
    lookwuliu(p).then(res=>{
      if(res.data.logisticsData.length==0){
        wx.showToast({
          title: '未查到物流信息',
          icon: 'error',
          duration: 2000
        })
      }else{
        console.log(res)
        this.setData({
          wuliulist:res.data.logisticsData,
          kuaidigongsi:res.data.typeName,
          kuaididanhao:res.data.number
        })
      }
      console.log(res)
    })

  },
  handchange(e){
    console.log(e.detail.current)
    let a=0;
    if(e.detail.current==0){a=6}
    if(e.detail.current==1){a=0}
    if(e.detail.current==2){a=1}
    if(e.detail.current==3){a=5}
    if(e.detail.current==4){a=3}
    if(e.detail.current==5){a=4}
    this.setData({
      currentTab: e.detail.current,
    });
    let p = {
      memberId:wx.getStorageSync('memberId'),
      pageNum:1,
      payStatus:a
    }   

    shebeilist(p).then(res=>{
      console.log(res)
      for(let i=0;i< res.data.datas.length;i++){
        let arr=[];
        arr= res.data.datas[i].aaEquipmentType.etImgs.split(",")
        res.data.datas[i].aaEquipmentType.arr=arr;
       
      }
     
      this.setData({
        list:res.data.datas
      })
      
    })
   
    
  
  },
  off(){
    this.hideModal()
    this.setData({
      show:false
    })
  },
  gopay(e){
   
    let p = {
      id:e.currentTarget.dataset.id,
      payType:0,
      type:0
     }
    payment(p).then(res=>{
      if(res.code==200){
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: res.data.signType,
        paySign: res.data.sign,
        success (res) {
          console.log("支付成功 ")
          wx.showModal({
            title:'提示',
            content:'支付成功',
            success (res) {
             if (res.confirm) {
               wx.navigateBack({
                 delta: 1,
               })
              }
           }
          })
         },
        fail (res) { 
          console.log("支付失败 ")
          wx.showToast({
            title: "支付失败",
            icon: 'error',
            duration: 2000
          })
        }
      })}else{
        wx.showToast({
          title: res.msg,
          icon: 'error',
          duration: 2000
        })
      }
     })
 
  },
  delete(e){
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定删除此订单',
      success (res) {
        if (res.confirm) {
          
          let p={
            memberId:wx.getStorageSync('memberId'),
            recordId:e.currentTarget.dataset.orderno
          }
          shanchu(p).then(res=>{
            that.onLoad()
            
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
 
  
   
  },
  cancel(e){
   let that=this
    wx.showModal({
      title: '提示',
      content: '确定取消此订单',
      success (res) {
        if (res.confirm) {
        
          let p={
            memberId:wx.getStorageSync('memberId'),
            recordId:e.currentTarget.dataset.orderno
          }
          quxiao(p).then(res=>{
            that.onLoad()
            
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  scrollToTop() {
    this.setAction({
      scrollTop: 0
    })
  },
  tabNav(e) { 
    this.setData({
      currentTab:e.currentTarget.dataset.current,
      memberId:wx.getStorageSync('memberId')
    })  
    let p = {
      memberId:wx.getStorageSync('memberId'),
      pageNum:1,
      payStatus:this.data.currentTab
    }   

    shebeilist(p).then(res=>{
      console.log(res)
      for(let i=0;i< res.data.datas.length;i++){
        let arr=[];
        arr= res.data.datas[i].aaEquipmentType.etImgs.split(",")
        res.data.datas[i].aaEquipmentType.arr=arr;
       
      }
      

      
      
      
      this.setData({
        list:res.data.datas
      })
      

    })

  },
  
  scan(options){
    let that = this
    wx.scanCode({
      success: (res)=>  {
       
        let code = res.result
        wx.showModal({
          title: '提示',
          content:'请点击确认激活设备',
          success (res) {
            if (res.confirm) {
              let p = {
                memberId:wx.getStorageSync('memberId'),
                eqCode:code,
                recordId:options.currentTarget.dataset.orderno,
                type:0
              }
              scanCodeBangding(p).then(res=>{
                if(res.code == 200)
                {
                  wx.showToast({
                    title: '激活成功',
                    icon:'none'
                  })
                  that.onLoad()
                }
                else
                {
                  wx.showToast({
                    title: res.msg,
                    icon:'none'
                  })
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    var that = this 
    wx.getSystemInfo({ 
        success: function (res) { 
            that.setData({ 
                clientHeight: res.windowHeight 
            }); 
        } 
    }) 


    let p = {
      memberId:wx.getStorageSync('memberId'),
      pageNum:1
    }
    shebeilist(p).then(res=>{
      for (let i = 0; i < res.data.datas.length; i++) {
        res.data.datas[i].hideScan = true
        switch(res.data.datas[i].payStatus)
        {
          case 0:
            res.data.datas[i].status='待付款'
          break
          case 1:
            res.data.datas[i].status='待发货'
          break
          case 5:
            res.data.datas[i].status='待安装'
          break
          case 3:
            res.data.datas[i].status='已完成'
            
          break
          case 4:
            res.data.datas[i].status='已取消'
            res.data.datas[i].hideScan = false
            break
        }
      }
      for(let i=0;i< res.data.datas.length;i++){
        let arr=[];
        arr= res.data.datas[i].aaEquipmentType.etImgs.split(",")
        res.data.datas[i].aaEquipmentType.arr=arr;
      }

      this.setData({
        list:res.data.datas
      })
      console.log(this.data.list)
     

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '分享小程序',
      imageUrl: 'https://anjingbao.anjingbao.net/profile/upload/renwufenxiang.jpg',
      path:"/pages/shouye/shouye",//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }
  }
})